package com.api.oa.mapper;


import com.api.oa.model.entity.OaAttend;
import com.api.oa.model.entity.OaOrgClassify;
import com.api.oa.model.vo.OaAttendStatics;
import com.api.oa.model.vo.OaAttendVO;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;


@Repository
@Mapper
public interface OaAttendMapper {


    @Insert("insert into aoa_attends_list (" +
            "attendsIp," +
            "attendsRemark," +
            "attendsTime," +
            "statusId," +
            "attendsUserId," +
            "weekOfDay," +
            "lat," +
            "address," +
            "attendsType," +
            "lng" +
            ") " +
            "values " +
            "(" +
            "#{business.attendsIp}," +
            "#{business.attendsRemark}," +
            "#{business.attendsTime}," +
            "#{business.statusId}," +
            "#{business.attendsUserId}," +
            "#{business.weekOfDay}," +
            "#{business.lat}," +
            "#{business.address}," +
            "#{business.attendsType}," +
            "#{business.lng}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "attendsId", keyColumn = "id")
    Integer insert(@Param("business") OaAttend business);


    @Update("update aoa_attends_list set classifyName = #{org.classifyName}, classifyPid=#{org.classifyPid} where attendsId = #{org.attendsId}")
    Integer update(@Param("org") OaAttend org);


    @Delete("delete from aoa_attends_list where attendsId=#{id}")
    void delete(@Param("id") Long id);


    @Select("<script>select aoa_attends_list.*,staff.RealName from aoa_attends_list left join staff on aoa_attends_list.attendsUserId = staff.StaffCode <if test='name!=null || name!=\"\"'> where staff.RealName like concat('%',#{name},'%')</if></script>")
    List<OaAttendVO> list(@Param("name") String userName);


    @Select("select * from aoa_attends_list where attendsId = #{id} limit 1")
    OaAttend findOne(@Param("id") Long id);


    @Select("select * from aoa_attends_list where attendsUserId = #{userCode} and DATE_format(attendsTime,'%Y-%m-%d') = #{today} order by attendsTime desc limit 1")
    OaAttend findOneByUserCodeAndToday(@Param("userCode") String userCode, @Param("today") String today);

    @Select("select * from aoa_attends_list where attendsUserId=#{uid} and DATE_format(attendsTime,'%Y-%m-%d') = #{today} order by attendsTime")
    List<OaAttendVO> listByUserToday(@Param("uid") String uid, @Param("today") String today);


    @Select("<script>select oa_attend_statics.*,staff.RealName,staff.StaffCode from oa_attend_statics right join staff on staff.StaffCode = oa_attend_statics.attendsUserId  and DATE_format(upTime,'%Y-%m-%d') = DATE_format(now(),'%Y-%m-%d') where GovernmentCode = #{govCode}  <if test='name!=null||name!=\"\"'> and staff.RealName like concat('%',#{name},'%')</if></script>")
    List<OaAttendStatics> listByToday(@Param("name") String name,@Param("govCode") String govCode);



    @Select("<script>select oa_attend_statics.*,staff.RealName,staff.StaffCode from oa_attend_statics right join staff on staff.StaffCode = oa_attend_statics.attendsUserId  where oa_attend_statics.attendsUserId = #{uid} and Date_format(upTime,'%Y-%m-%d') between #{start} and #{end}</script>")
    List<OaAttendStatics> listByWeek(@Param("uid") String uid,@Param("start") String start,@Param("end")String end);

}
